package com.kintreda.ieco_server.module.api.web;

import com.kintreda.ieco_server.core.web.R;
import com.kintreda.ieco_server.util.StringUtil;
import com.kintreda.ieco_server.util.sms.SendMessage;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Created by IntelliJ IDEA
 * @Project_Name ieco_server
 * @Package com.kintreda.ieco_server.module.api.web
 * @Author ZhongYao_H
 * @Date 2019/5/10:15:26
 * @Year 2019 Month 05 Day 10
 * @Date_zh 2019 年 五月 星期五
 */
@Controller
@RequestMapping("/api/send")
public class ApiSendMsdController {

    @RequestMapping(value = "/sms",method = RequestMethod.POST)
    @ResponseBody
    public Object sendmsg(String moblie){
        String code = StringUtil.getRandomNum(4);//五位随机数
        String httpResponse = SendMessage.SendMsg(moblie,code);
        try {

            JSONObject jsonObj = new JSONObject( httpResponse );
            int error_code = jsonObj.getInt("error");
            String error_msg = jsonObj.getString("msg");
            if(error_code==0){
                System.out.println("发送成功! 验证码:"+code);
                return R.ok("下发短信成功,请注意查收").put("vcode",code);
            }else{
                System.out.println("发送失败! 错误码="+error_code+", 错误信息= "+error_msg);
                return  R.error("系统异常稍后重试");
            }

        } catch (JSONException ex) {
            return  R.error("系统异常稍后重试");
        }


    }
}
